package com.brand.data.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;

/**
 * @author scl.BlueOcean
 * @version 1.0
 * @date 2018年04月20 14:08
 * @description 极验验证码识别服务
 */
@Slf4j
public class GeeTestUtil {
    private static final String USER="cxmtest";
    private static final String PASS="cxm123321!";
    private static final String URL="http://jiyanapi.c2567.com/shibie";
    /** 验证码识别  */
    public static String  validate(String geeBody) throws IOException {
        JSONObject obj = JSON.parseObject(geeBody);
        Connection conn = Jsoup.connect(URL).ignoreContentType(true).timeout(30000);
        conn.data("user", USER);
        conn.data("pass", PASS);
        conn.data("gt", obj.getString("gt"));
        conn.data("challenge", obj.getString("challenge"));
//        conn.data("referer", "https://dcs.gacfcasales.com");
        conn.data("return", "json");
        conn.data("model", "3");
        String res = conn.method(Connection.Method.GET).execute().body();
        log.info("验证码识别结果,{}",res);
        return res;
    }


    private static String yzmUrl = "http://jiyan.25531.com/api/create";
    private static String APP_KEY = "65cfc98f65f084aa451479d6a31e7132";

    /** 验证码识别  */
    public static String validate2(String geeBody) throws IOException{
        JSONObject obj = JSON.parseObject(geeBody);
        Connection conn = Jsoup.connect(yzmUrl).ignoreContentType(true).timeout(30000);
        conn.data("appkey", APP_KEY);
        conn.data("gt", obj.getString("gt"));
        conn.data("challenge", obj.getString("challenge"));
        //conn.data("referer", "https://dcs.gacfcasales.com");
        conn.data("model", "3");
        String res = conn.method(Connection.Method.GET).execute().body();
        log.info("验证码识别结果,{}",res);
        JSONObject object = JSON.parseObject(res);
        if (!"10000".equals(object.getString("code"))) {
			return validate(geeBody);
		}
        return object.getString("data");
    }
}
